package com.dingreading.cloud.mall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.mall.dto.RealUsableNumDto;
import com.dingreading.cloud.mall.entity.WhStockIn;
import com.dingreading.cloud.mall.entity.table.WhStockInTableDef;
import com.dingreading.cloud.mall.mapper.WhStockInMapper;
import com.dingreading.cloud.mall.service.WhStockInService;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class WhStockInServiceImpl extends BaseServiceImpl<WhStockInMapper, WhStockIn, WhStockInTableDef> implements WhStockInService {

    @Override
    protected WhStockInTableDef getTable() {
        return WhStockInTableDef.whStockIn;
    }


    @Override
    public Page<WhStockIn> pageList(PageUtil pageUtil, String keyword, Integer status, String beginDate, String endDate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword)) {
            condition.and(table.purchaseCode.like(keyword)
                    .or(table.inCode.like(keyword))
                    .or(table.goodsName.like(keyword))
                    .or(table.goodsTypeName.like(keyword))
                    .or(table.supplierName.like(keyword))
            );
        }

        if (status != null)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public Map<Long, Integer> getStockNumMap(List<Long> goodsTypeIds) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.goodsTypeId.in(goodsTypeIds))
                .and(table.usableNum.gt(0))
                .and(table.status.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.goodsTypeId, QueryMethods.sum(table.usableNum).as("usableNum"))
                .from(table)
                .where(condition)
                .groupBy(table.goodsTypeId);

        List<Row> rows = getMapper().selectRowsByQuery(wrapper);

        Map<Long, Integer> map = new HashMap<>();
        for (Long goodsTypeId : goodsTypeIds) {
            map.put(goodsTypeId, 0);
        }
        if (CollectionUtils.isNotEmpty(rows)) {
            for (Row row : rows) {
                Long aLong = row.getLong("goods_type_id");
                Integer integer = row.getInt("usableNum");
                if (aLong != null)
                    map.put(aLong, NepUtil.nullToZero(integer));
            }
        }
        return map;
    }

}
